package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Fillerfiller;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FillerfillerDAO")
@Transactional
public class FillerfillerDAOImpl extends AbstractJpaDao<Fillerfiller> implements FillerfillerDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Fillerfiller.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FillerfillerDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Fillerfiller findFillerfillerById(Integer id) throws DataAccessException {

		return findFillerfillerById(id, -1, -1);
	}

	@Transactional
	public Fillerfiller findFillerfillerById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerfillerById", startResult, maxRows, id);
			return (Fillerfiller) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillerByName(String name) throws DataAccessException {

		return findFillerfillerByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findFillerfillerByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerfillerByName", startResult, maxRows, name);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillerByNameContaining(String name) throws DataAccessException {

		return findFillerfillerByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findFillerfillerByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerfillerByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillerBySimilarity(String similarity) throws DataAccessException {

		return findFillerfillerBySimilarity(similarity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findFillerfillerBySimilarity(String similarity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerfillerBySimilarity", startResult, maxRows, similarity);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillerBySimilarityContaining(String similarity) throws DataAccessException {

		return findFillerfillerBySimilarityContaining(similarity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findFillerfillerBySimilarityContaining(String similarity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerfillerBySimilarityContaining", startResult, maxRows, similarity);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillerByDescription(String description) throws DataAccessException {

		return findFillerfillerByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findFillerfillerByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerfillerByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillerByDescriptionContaining(String description) throws DataAccessException {

		return findFillerfillerByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findFillerfillerByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerfillerByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@Transactional
	public Fillerfiller findFillerfillerByPrimaryKey(Integer id) throws DataAccessException {
		return findFillerfillerByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Fillerfiller findFillerfillerByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerfillerByPrimaryKey", startResult, maxRows, id);
			return (Fillerfiller) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillersByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerfillersByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillerfiller> findAllFillerfillers() throws DataAccessException {

		return findAllFillerfillers(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findAllFillerfillers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFillerfillers", startResult, maxRows);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Fillerfiller> findFillerfillersByOther(Integer fillerId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFillerfillersByOther", start, max, fillerId);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Fillerfiller> findFillerfillersBySelf(Integer fillerId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFillerfillersBySelf", start, max, fillerId);
		return new LinkedHashSet<Fillerfiller>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillerfiller> findFillerfillerByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Fillerfiller>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Fillerfiller> findFillerfillersByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Fillerfiller.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Fillerfiller> fillerfillers = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return fillerfillers;
	}

	@Transactional
	public Fillerfiller findFillerfillerByPhysicalPrimaryKey(Integer selfId, Integer otherId)
			throws DataAccessException {
		return findFillerfillerByPhysicalPrimaryKey(selfId, otherId, -1, -1);
	}

	@Transactional
	public Fillerfiller findFillerfillerByPhysicalPrimaryKey(Integer selfId, Integer otherId, int startResult,
			int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerfillerByPhyscialPrimaryKey", startResult, maxRows, selfId,
					otherId);
			return (Fillerfiller) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Fillerfiller entity) {
		return true;
	}
}
